<?php

/**
 * $Id$
 */
class SGN_HandlerConfig
{
    /**
     * Contains array of configuration data
     *
     * @var array
     */
    protected $_data;

    /**
     * SGN_Config provides a property based interface to
     * an array.
     *
     * @param array $array            
     * @return void
     */
    public function __construct(array $array)
    {
        $this->_data = array();
        foreach ($array as $key => $value) {
            if (is_array($value)) {
                $this->_data[$key] = new self($value);
            } else {
                $this->_data[$key] = $value;
            }
        }
    }

    /**
     * Retrieve a value and return $default if there is no element set.
     *
     * @param string $name            
     * @param mixed $default            
     * @return mixed
     */
    public function get($name, $default = null)
    {
        $result = $default;
        if (array_key_exists($name, $this->_data)) {
            $result = $this->_data[$name];
        }
        return $result;
    }

    /**
     * Magic function so that $obj->value will work.
     *
     * @param string $name            
     * @return mixed
     */
    public function __get($name)
    {
        return $this->get($name);
    }

    /**
     * Support isset() overloading on PHP 5.1
     *
     * @param string $name            
     * @return boolean
     */
    public function __isset($name)
    {
        return isset($this->_data[$name]);
    }

    /**
     * Support unset() overloading on PHP 5.1
     *
     * @param string $name          
     * @return void
     */
    public function __unset($name)
    {
        unset($this->_data[$name]);
    }
    
    /**
     * Explode to array
     */
    public function toArray()
    {
    	$res = array();
    	foreach($this->_data as $key => $value) {
    		if ($value instanceof SGN_HandlerConfig) {
    			$res[$key] = $value->toArray();
    		} else {
    			$res[$key] = $value;
    		}
    	}
    	return $res;
    }
}
